import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { BuffModelComp } from "./model/BuffModelComp";
import { BattleTriggerEvent, Effect } from "../scene/model/BattleEnum";
import { BuffIconViewComp } from "./view/BuffIconViewComp";
import { ViewUtil } from "db://oops-framework/core/utils/ViewUtil";
import { Role } from "../../role/Role";
import { EffectBuff } from "../skill/model/report/EffectBuff";
import { RichText, v3 } from "cc";
import { EffectSingleCase } from "db://oops-framework/libs/animator-effect/EffectSingleCase";
import { RoleViewComp } from "../../role/view/RoleViewComp";
import { BuffViewComp } from "./view/BuffViewComp";

/** 状态系统 */
@ecs.register(`Buff`)
export class Buff extends ecs.Entity {
    // 数据层
    BuffModel!: BuffModelComp;

    // 视图层
    BuffIconView!: BuffIconViewComp;
    protected init() {
        this.addComponents<ecs.Comp>(BuffModelComp);
    }


    destroy(): void {
        this.remove(BuffViewComp);
        super.destroy();
    }
    /**
        * 技能施放数值计算
        * @param effect    施放BUFF的技能
        * @param caster    施放BUFF的角色
        * @param target    承受BUFF的角色
        * @param event     触发事件
        */
    cast(event: BattleTriggerEvent, effect?: Effect) {
        var casting = this.BuffModel.casting;
        casting.buff = this;
        if (effect) {
            casting.effect = effect;
        }

        // 释放技能，施放条件在重写方法中自定义
        (casting as any)[event]();
    }

     /** 加载buff特效 */
     loadBuffEffect(target: Role, effect: EffectBuff, delay: number = 0) {
        if(!effect.buff.BuffModel || !target?.RoleView){
            return
        }
        setTimeout(() => {
            this.showBuffName(target, effect);
        }, delay * 100);
        
        var effectBuff = effect.buff.BuffModel.table.effect_buff;
        if(effectBuff){
            if(this.has(BuffViewComp)){
                return
            }
            var node = ViewUtil.createPrefabNode(`game/buff/buff_effect`);
            this.add(node.getComponent(BuffViewComp)!);
            node.getComponent(BuffViewComp)?.load(target, {anim: effectBuff, loop: 1});
        }
    }

    /**
    * 显示Buff名
    * @param target 特效显示的目标角色身上
    */
    private showBuffName(target: Role, effect: EffectBuff) {
        // 如果buff没有模型或者模型没有tip，则返回
        if(!effect.buff.BuffModel || !effect.buff.BuffModel.table.tip){
            return
        }
        // 如果目标没有视图或者视图没有节点，则返回
        if(!target || !target.RoleView || !target.RoleView.node){
            return
        }
        // 定义一个位置变量
        var pos = v3(0, 0);
        // 定义一个路径变量
        var path = "game/blood/buff_name";
        // 定义一个父节点变量
        var parent = target.RoleView.node
        // 定义一个变量，表示是否播放完毕释放
        var isPlayFinishedRelease = true;
        // 显示特效
        var node = EffectSingleCase.instance.show(path, parent, { pos, isPlayFinishedRelease });
        // 获取特效中的富文本组件
        var lab: RichText = node.getChildByName("lab_name")?.getComponent(RichText)!;
        // 设置富文本组件的文本
        lab.string = effect.buff.BuffModel.table.tip;

        // 如果buff没有BuffIconViewComp组件，并且有icon_path，则创建一个BuffIconViewComp组件，并添加到buff中
        if(!effect.buff.has(BuffIconViewComp) && effect.buff.BuffModel.table.icon_path){
            // 创建一个BuffIconViewComp组件
            var node = ViewUtil.createPrefabNode(`game/buff/buff_icon`);
            // 将BuffIconViewComp组件添加到buff中
            effect.buff.add(node.getComponent(BuffIconViewComp)!);
            // 设置位置
            node.setPosition(v3(0, 0, 0));
            // 将BuffIconViewComp组件添加到RoleViewComp组件的layout_buff_icon节点中
            node.parent = target.RoleView.getComponent(RoleViewComp)!.get("layout_buff_icon")!;
        }
    }


    /** 是否眩晕BUFF */
    isDizzy(): boolean{
        return this.BuffModel.table.id == 3;
    }

    /** 是否冰冻BUFF */
    isFrozen(): boolean{
        return this.BuffModel.table.id == 2;
    }
}

export class EcsBuffSystem extends ecs.System {
    constructor() {
        super();

    }
}
